package com.hktf.corelib.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yubl
 * @version 1.0
 * @todo 把log写入文件中
 * @date 创建时间：2016-3-23 上午10:33:12
 */
public class LogWriter {


    private static LogWriter mLogWriter;

    private static String mPath;

    private static Writer mWriter;

    private static SimpleDateFormat df;

    private LogWriter(String file_path) {
        this.mPath = file_path;
        this.mWriter = null;
    }

    public static LogWriter open(String file_path) throws IOException {
        if (mLogWriter == null) {
            mLogWriter = new LogWriter(file_path);
        }
        File mFile = new File(mPath);
        mWriter = new BufferedWriter(new FileWriter(mPath), 2048);
        df = new SimpleDateFormat("[yy-MM-dd hh:mm:ss:SSS]:");

        return mLogWriter;
    }

    public static void close() throws IOException {
        mWriter.close();
    }

    public static void print(String log) {
        try {
            //LogMds.e("logwriter",log);
            if (df == null) {
                return;
            }
            mWriter.write(df.format(new Date()));
            mWriter.write(log);
            mWriter.write("\n");
            mWriter.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void print(Class cls, String log) { //如果还想看是在哪个类里可以用这个方法
        try {
            mWriter.write(df.format(new Date()));
            mWriter.write(cls.getSimpleName() + " ");
            mWriter.write(log);
            mWriter.write("\n");
            mWriter.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
